<?php
namespace App\Services\SearchService;
/**
 * 后台筛选
 *
 * 后台筛选Service
 *
 * @author    zhouchao <zhouchao@yunys.com.cn>
 * @copyright 2016 QuanZhouYunYiShang Inc.
 */
use App\Services\BaseService;
use App\Services\ProductsService\ProductTool;
use DB;
class SearchBackService extends BaseService
{
    protected $productTool;
    public function __construct(ProductTool $productTool)
    {
        $this->productTool = $productTool;
    }
    public function getList($product_temp_category_id,$per_page = 10){
        $this->setParameter([
            'table' => 'searchs',
            'where' => [['product_temp_categoty_id','=',$product_temp_category_id],['pid','=',0],['state','=',1]],
            'select'=> ['search_id','product_temp_categoty_id','search_name','resource_id','level','pid','type','is_radio','sort'],
            'order' => 'sort',
            'sort'  => 'desc'
        ]);
        $searchs = $this->getAllPaginated($per_page);
        foreach($searchs as $search){
            $this->setParameter([
                'table' => 'searchs',
                'where' => [['pid','=',$search->search_id],['state','=',1]],
                'select'=> ['search_id','product_temp_categoty_id','search_name','resource_id','level','pid','type','is_radio','sort'],
                'order' => 'sort',
                'sort'  => 'desc'
            ]);
            $search_children = $this->getAll();
            $search->search_children = $search_children;
        }
        return $searchs;
    }
    public function get_search_by_search_id($search_id){
        return  DB::table('searchs')
            ->where('search_id',$search_id)
            ->where('state',1)
            ->first();
    }
    public function save_search($data){
        if($data['pid'] == 0){
            //创建顶级分类
            $searchs = DB::table('searchs')
                ->where('product_temp_categoty_id',$data['product_temp_categoty_id'])
                ->where('search_name',$data['search_name'])
                ->where('state',1)
                ->first();
            if(empty($searchs)){
                if(empty($data['type'])){
                    return array('message'=>'请选择分类','result'=>'','state'=>'M00001');
                }
                if($data['type'] == 1){
                    //价格区间
                    $update_data['level'] = empty($data['level']) || $data['level']>10 ? 1 : $data['level'];
                }
                $update_data['product_temp_categoty_id'] = $data['product_temp_categoty_id'];
                $update_data['search_name'] = $data['search_name'];
                $update_data['type'] = $data['type'];
                $update_data['resource_id'] = 0;
                $update_data['is_radio'] = $data['is_radio'];
                $update_data['created_at'] = $update_data['updated_at'] = time();
                return DB::table('searchs')->insertGetId($update_data);
            }else{
                return array('message'=>'搜索名称已存在','result'=>'','state'=>'M00001');
            }
        }else{
            //二级分类
            $searchs = DB::table('searchs')
                ->where('search_id',$data['pid'])
                ->where('state',1)
                ->first();
            if(empty($searchs)){
                return false;
            }
            //判断search_name
            $child_searchs = DB::table('searchs')
                ->where('pid',$data['pid'])
                ->where('search_name',$data['search_name'])
                ->where('state',1)
                ->first();
            if(empty($child_searchs)){
                if(empty($data['resource_id'])){
                    return array('message'=>'参数错误','result'=>'','state'=>'M00001');
                }
                $update_data['product_temp_categoty_id'] = $searchs->product_temp_categoty_id;
                $update_data['search_name'] = $data['search_name'];
                $update_data['type'] = $searchs->type;
                $update_data['resource_id'] = $data['resource_id'];
                $update_data['pid'] = $data['pid'];
                $update_data['is_radio'] =  $searchs->is_radio;
                $update_data['created_at'] = $update_data['updated_at'] = time();
                return DB::table('searchs')->insertGetId($update_data);
            }else{
                return array('message'=>'搜索名称已存在','result'=>'','state'=>'M00001');
            }
        }
    }
    /**
     * 获取款式列表
     * @return array
     */
    public function get_all_style_list(){
        $product_temp_attributes = DB::table('product_temp_attributes')
            ->where('state',1)
            ->pluck('attr_values')
            ->toArray();
        $attrs = array();
        $attrs_used = array();
        foreach($product_temp_attributes as $product_temp_attribute){
            $product_temp_attribute = explode('|',$product_temp_attribute);
            foreach($product_temp_attribute as $item){
                if(!in_array($item,$attrs_used)){
                    $obj = (object)array();
                    $obj->attr_name = $item;
                    $attrs[] = $obj;
                    $attrs_used[] = $item;
                }
            }
            //$attrs = array_merge($attrs,$product_temp_attribute);
        }
        //$attrs = array_unique($attrs);
        return $attrs;
    }
    public function delete_search($search_id){
        $children_search = DB::table('searchs')->where('pid',$search_id)->where('state',1)->get()->toArray();
        if(!empty($children_search)){
            return array('message'=>'该筛选下还有子筛选，不能删除','result'=>'','state'=>'M00001');
        }
        $result = DB::table('searchs')
            ->where('search_id',$search_id)
            ->update(['state'=>2]);
        return empty($result) ? array('message'=>'删除失败','result'=>'','state'=>'M00001') :
            array('message'=>'删除成功','result'=>'','state'=>'M00000');
    }

    public function update_search_sort($search_id,$sort){
        $this->setParameter(['table'=>'searchs','where'=>[['search_id','=',$search_id]],['state','=',1]]);
        return $this->update(array('sort'=>$sort,'updated_at'=>time()));
    }
}